/**
 * モンスター
 *
 * LICENSE: Miracle Maple by Mirage is licensed
 * under a Creative Commons 表示-非営利-継承 2.1 日本 License.
 *
 * @category   js
 * @package    maple-story.jp
 * @version    8.0.0
 * @link       http://maple-story.jp/
 * @since      File available since Release 8.0.0
 * @copyright  Copyright 2011 Miracle Maple
 * @license    http://creativecommons.org/licenses/by-nc-sa/2.1/jp/     Creative Commons BY-NC-SA
 */

$(function() {
	// get the first collection
	var $applications = $('section.result-list ul');
	// clone applications to get a second collection
	var $data = $applications.clone();
	
	var quicksandDefaultOption = {
		duration: 800,
		easing: 'easeInOutQuad',
		adjustHeight: 'dynamic'
	}
	
	$("#search-config form").submit(function(e) {
		e.preventDefault();
	});
	
	$("#search-config dl dt").click(function() {
		$("#search-config dl dd").hide();
		$(this).next().show();
	});
	
	if ($("#level-range").val() != "Lv 1 - 200") {
		$("dt#condition-level").addClass("search-available");
	}
	
	$("input.set-value").click(function(e) {
		$("span#result-desc").html("<img src='/image/common/loading.gif' class='loading' width='220' height='19'>");		
		
		// 設定内容を検索条件ブロックに反映
		var $source = $(this).prev().val();
		var $target = $(this).parent().parent().parent().find("dt");
		if ($source != "") {
			$target.html($source);
		} else {
			$target.html($(this).prev().attr("placeholder"));
		}
		$("#search-config dl dd").hide();

		// Ajax 検索
		$.get(getAjaxUrl(), function(data) {
			$applications.quicksand(
				$(data).find('.result-list li'), quicksandDefaultOption, function() {
					// リスト内容オブジェクトの更新
					$applications = $('section.result-list ul');
					$data = $applications.clone();

					// 属性絞り込みが有効な状態での Ajax 検索対応
					filterByElemAttr($filterElemAttr, $applications, $data);
				}
			);
			updateResultDescription($(data).find('.result-list li').length);
		});
	});

	// キーワード	
	$("input#search-keyword").keydown(function(e) {
		if (e.keyCode == 13) {
			$(this).next().click();
		}
	});
	
// attempt to call Quicksand on every form change
//	var $filterSort = $('#filter input[name="sort"]');
//	$filterType.add($filterSort).change(function(e) {
	
	// 属性耐性 フィルタリング
	var $filterElemAttr = $('dl.element-attr dt');
	$filterElemAttr.click(function(e) {
		changeElementAttrOption(e.target);
		filterByElemAttr($filterElemAttr, $applications, $data);
	});
	
	/**
	 * 属性絞り込み
	 * @return	void
	 */
	function filterByElemAttr($elements, $source, $destination)
	{
		var elemAttrSelector = "";
		$elements.each(function() {
			if (!$(this).hasClass("search-available")) {
				return true;
			}
			
			var attrType = $(this).attr("id").replace("search-elem-", "");

			if ($(this).text().indexOf("◎") >= 0) {
				elemAttrSelector += "[data-elem-attr*='" + attrType + "3']";
			} else if ($(this).text().indexOf("○") >= 0) {
				elemAttrSelector += ":not([data-elem-attr*='" + attrType + "'])";
			} else if ($(this).text().indexOf("△") >= 0) {
				elemAttrSelector += "[data-elem-attr*='" + attrType + "2']";
			} else if ($(this).text().indexOf("×") >= 0) {
				elemAttrSelector += "[data-elem-attr*='" + attrType + "1']";
			}
		});
		
		var $filteredData = $destination.find("li");
		if (elemAttrSelector != "") {
			$filteredData = $destination.find("li" + elemAttrSelector);
		}
		
		updateResultDescription($filteredData.length);
		$source.quicksand($filteredData, quicksandDefaultOption);
	}
	
	/**
	 * 属性切り替え
	 * @param	target	jQuery.Event.Target	押した属性ボタン
	 * @return	void
	 */
	function changeElementAttrOption(target)
	{
		var t = $(target);
		var tx = t.text();
		
		t.addClass("search-available");
		
		if (tx.indexOf("—") >= 0) {
			t.text(tx.replace("—", "◎"));
		} else if (tx.indexOf("◎") >= 0) {
			t.text(tx.replace("◎", "○"));
		} else if (tx.indexOf("○") >= 0) {
			t.text(tx.replace("○", "△"));
		} else if (tx.indexOf("△") >= 0) {
			t.text(tx.replace("△", "×"));
		} else if (tx.indexOf("×") >= 0) {
			t.text(tx.replace("×", "—"));
			t.removeClass("search-available");
		}
	}
	
	/**
	 * 検索結果説明文の更新
	 * @param		int	resultCount		検索結果件数
	 */
	function updateResultDescription(resultCount)
	{
		if (resultCount > 0) {
			$("span#result-desc").html("(" + resultCount + " 件のモンスターが該当しました)");
		} else {
			$("span#result-desc").html("(該当モンスターなし)");
		}
	}
	
	/**
	 * Ajax 検索データ取得用 URL
	 */
	function getAjaxUrl()
	{
		var searchUrl = "/monster/search/";
		if ($("#search-keyword").val() != "") {
			searchUrl += "name/" + $("#search-keyword").val() + "/";
			$("dt#condition-keyword").addClass("search-available");
		} else {
			$("dt#condition-keyword").removeClass("search-available");
		}
		
		if ($("#level-range").val() != "" && $("#level-range").val() != "Lv 1 - 200") {
			searchUrl += "lv/" + $("#level-range").val().replace("Lv ", "") + "/";
			$("dt#condition-level").addClass("search-available");
		} else {
			$("dt#condition-level").removeClass("search-available");
		}
		
		if ($("input[name=search-option]:checked").length > 0) {
			var arrOption = [];
			$("input[name=search-option]:checked").each(function() {
				arrOption.push($(this).val());
			});
			searchUrl += "option/" + arrOption.join("-") + "/";
			$("dt#condition-option").addClass("search-available");
		} else {
			$("dt#condition-option").removeClass("search-available");
		}
		
		if ($("input[name=search-option-rare]:checked").val() != "") {
			searchUrl += "rare/" + $("input[name=search-option-rare]:checked").val() + "/";
			$("dt#condition-option").addClass("search-available");
		}
		
		if ($("input[name=search-sort]:checked").val() != "") {
			searchUrl += "order/" + $("input[name=search-sort]:checked").val() + "/";
			$("dt#condition-sort").addClass("search-available");
		} else {
			$("dt#condition-sort").removeClass("search-available");
		}
		
		return searchUrl;
	}
});